# encoding: utf-8
# @author: w4dll
# @file: test_pane.py
# @time: 2020/4/29 19:39

from resource.moniUI import Ui_Form
from PyQt5 import sip
from functools import partial
from mytool import *

class moniPane(QWidget, Ui_Form):

    test_finished_signal = pyqtSignal(list)  # 考试结束信号

    def __init__(self, data, name, parent = None, *args, **kwargs):
        super(moniPane, self).__init__(parent, *args, **kwargs)
        self.setupUi(self)

        self.name = name
        self.tkdata = data              # 考题
        self.forward_btnNum = 0         # 这个要放在initUi前面

        # 初始化界面题号
        self.initUi()
        self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)


    # 根据用户传入的题库信息dict_tiku初始化考试界面
    def initUi(self):
        self.scrollAreaWidgetContents.setMinimumSize(1000, 50 + int(26 * len(self.tkdata) / 25))

        # todo 初始化按钮界面
        for i in range(len(self.tkdata)):
            btn = QPushButton(self.scrollAreaWidgetContents)
            btn.resize(35, 20)  # 设置按钮的高度和宽度
            btn.setText(str(i + 1))
            btn.setFocusPolicy(Qt.NoFocus)

            self.win_width = self.scrollAreaWidgetContents.width()  # 这个值是变化的，所以这里存储便于后面使用

            x = 10 + (i % int(self.win_width / 40)) * 38  # 按钮宽度+间隔=40，高度+间隔=30
            y = 10 + int(i / int(self.win_width / 40)) * 26
            btn.move(x, y)

            btn.clicked.connect(partial(self.btn_pressed, i))  # 这个函数可以实现参数传递
            if len(self.tkdata[i]) == 17:
                self.tkdata[i] += ("", )
            i += 1
            self.btn_pressed(0)  # 调用一下第一个按钮，显示初始题目状态

    # 题号按钮点击槽函数
    def btn_pressed(self, n):  # n->表示题目索引号，从0开始

        # 如果当前题有答案，显示答案
        item = self.tkdata[n]
        if len(item[-1]) > 0:
            self.label_ans.setText("正确答案："+ item[-3])
        else:
            self.label_ans.setText("正确答案：")

        # 恢复前一个按钮颜色
        x = 10 + (self.forward_btnNum % int(self.win_width / 40)) * 38  # 按钮宽度+间隔=40，高度+间隔=30
        y = 10 + int(self.forward_btnNum / int(self.win_width / 40)) * 26
        ans_prev = self.tkdata[self.forward_btnNum]
        if len(ans_prev[-1]) > 0: # 题目已经有答案，按钮蓝色
            if ans_prev[-1] == ans_prev[-3]:
                self.scrollAreaWidgetContents.childAt(x, y).setStyleSheet("background-color:cyan;")
            else:
                self.scrollAreaWidgetContents.childAt(x, y).setStyleSheet("background-color:red;")
        else: # 题目没有选择答案，恢复原样
            self.scrollAreaWidgetContents.childAt(x, y).setStyleSheet("")
        self.forward_btnNum = n   # 存储当前按钮号，下次使用

        # 设置当前按钮颜色
        x = 10 + (n % int(self.win_width/40)) * 38  # 按钮宽度+间隔=40，高度+间隔=30
        y = 10 + int(n / int(self.win_width/40)) * 26
        self.scrollAreaWidgetContents.childAt(x, y).setStyleSheet("background-color: rgb(255, 255, 0);")

        # 清空题目、选项、答案框内容
        if len(self.scrollAreaWidgetContents_2.children()) > 0 :
            for child in self.scrollAreaWidgetContents_2.children(): # 逐个删除题目
                sip.delete(child)
        if len(self.scrollAreaWidgetContents_3.children()) > 0 :
            for child in self.scrollAreaWidgetContents_3.children(): # 逐个删除选项
                sip.delete(child)
        if len(self.scrollAreaWidgetContents_4.children()) > 0 :
            for child in self.scrollAreaWidgetContents_4.children(): # 逐个删除答案
                sip.delete(child)

        # 设置题目显示信息
        s1 = str(self.tkdata[n][3])
        label_timu = QLabel(s1, self.scrollAreaWidgetContents_2)
        label_timu.move(10, 10)
        label_timu.show()

        # 设置选项显示信息
        if self.tkdata[n][4] == "单选题":
            s, i = "", 0
            for x in self.tkdata[n][5:-3]:
                if x != "":
                    s = s + chr(i + 65) + ". " + str(x) + "\n"
                    radio = QRadioButton(chr(i + 65), self.scrollAreaWidgetContents_4)
                    radio.move(10 + 50 * i, 10)  # 选项之间的距离50
                    radio.show()
                    radio.setFocusPolicy(Qt.NoFocus)
                    radio.clicked.connect(partial(self.radio_slot, n))  # 把题号索引传递过去
                    if len(self.tkdata[n][-1])>0:
                        if ord(self.tkdata[n][-1]) - 65 == i:
                            radio.setChecked(True)
                    i += 1
            # 显示题目
            label_timu = QLabel(s, self.scrollAreaWidgetContents_3)
            label_timu.move(10, 10)
            label_timu.show()
        elif self.tkdata[n][4] == "多选题":
            li = [ord(x) - 65 for x in self.tkdata[n][-1]]
            s, i = "", 0
            for x in self.tkdata[n][5:-3]:
                if x != "":
                    s = s + chr(i + 65) + ". " + str(x) + "\n"
                    chk = QCheckBox(chr(i + 65), self.scrollAreaWidgetContents_4)
                    chk.move(10 + 50 * i, 10)  # 选项之间的距离50
                    chk.show()
                    chk.setFocusPolicy(Qt.NoFocus)
                    chk.clicked.connect(partial(self.check_slot, n))  # 把题号索引传递过去
                    if len(self.tkdata[n][-1])>0:
                        if i in li:
                            chk.setChecked(True)
                    i += 1
            # 显示题目
            label_timu = QLabel(s, self.scrollAreaWidgetContents_3)
            label_timu.move(10, 10)
            label_timu.show()
        elif self.tkdata[n][4] == "判断题":
            s, i = "", 0
            for x in self.tkdata[n][5:-3]:
                if x != "":
                    s = s + chr(i + 65) + ". " + str(x) + "\n"
                    radio = QRadioButton(chr(i + 65), self.scrollAreaWidgetContents_4)
                    radio.move(10 + 50 * i, 10)  # 选项之间的距离50
                    radio.show()
                    radio.setFocusPolicy(Qt.NoFocus)
                    radio.clicked.connect(partial(self.radio_slot, n))  # 把题号索引传递过去
                    if len(self.tkdata[n][-1])>0:
                        if ord(self.tkdata[n][-1]) - 65 == i:
                            radio.setChecked(True)
                    i += 1
            # 显示题目
            label_timu = QLabel(s, self.scrollAreaWidgetContents_3)
            label_timu.move(10, 10)
            label_timu.show()
        elif self.tkdata[n][4] == "填空题":
            let = QLineEdit(self.scrollAreaWidgetContents_4)
            let.resize(self.scrollAreaWidgetContents_4.width(), self.scrollAreaWidgetContents_4.height())
            let.textChanged.connect(partial(self.text_changed, n))
            let.show()
            if self.tkdata[n][-1] == "":
                let.setPlaceholderText("请输入答案，并用分号（；）隔开")
            else:
                let.setText(self.tkdata[n][-1])
            let.setFocus()
        elif self.tkdata[n][4] == "问答题":
            let = QTextEdit(self.scrollAreaWidgetContents_4)
            let.resize(self.scrollAreaWidgetContents_4.width(), self.scrollAreaWidgetContents_4.height())
            let.textChanged.connect(partial(self.text_changed1, n))
            let.show()
            if self.tkdata[n][-1] == "":
                let.setPlaceholderText("请输入你的答案！")
            else:
                let.setText(self.tkdata[n][-1])
                # let.toPlainText()
            let.setFocus()
        else:
            pass

    # 填空题槽函数
    def text_changed(self, n):
        text = self.scrollAreaWidgetContents_4.children()[0].text()
        self.tkdata[n] = self.tkdata[n][:-1] + (text,)

    # 填空题槽函数
    def text_changed1(self, n):
        text = self.scrollAreaWidgetContents_4.children()[0].toPlainText()
        self.tkdata[n] = self.tkdata[n][:-1] + (text,)


    # 多选选项槽函数
    def check_slot(self, n):
        s = ""
        for chk in self.scrollAreaWidgetContents_4.children():
            if chk.isChecked():
                s += chr(self.scrollAreaWidgetContents_4.children().index(chk) + 65)
        self.tkdata[n] = self.tkdata[n][:-1] + (s,)

    # 单选选项槽函数，判断题共用
    def radio_slot(self, n):
        for radio in self.scrollAreaWidgetContents_4.children():
            s = ""
            if radio.isChecked():
                s = s + chr(self.scrollAreaWidgetContents_4.children().index(radio) + 65)
                self.tkdata[n] = self.tkdata[n][:-1] + (s,)

    # 提交考试槽函数
    def submit_test(self):
        if QMessageBox.Yes == QMessageBox.question(self, "提示", "是否存储错题？"):
            wrong, right = [], []
            for key in self.tkdata:
                if key[-1] != "":
                    if key[-1] != key[-3]:
                        wrong.append(key)
                    else:
                        right.append(key)

            # 如果错题集存在，存入，答错次数+1
            data = sqlite3Class("data.db")
            my_wrong = data.selectdata("select * from t_wrongtiku")
            for x in wrong:
                m = 0
                for y in my_wrong:
                    if (self.name, )+x[1:-1] == y[1:-2]:
                        num = y[-1] + 1
                        sql = "update t_wrongtiku set 答错次数='"+str(num)+"' where ID='"+str(y[0])+"'"
                        data.excuteSql(sql)
                        m = 1
                if m != 1:
                    sql = "insert into t_wrongtiku(用户名,适用人员,专业分类,题干,题型,选项1,选项2,选项3,选项4,选项5,选项6," \
                          "选项7,选项8,选项9,选项10,答案,备注,我的答案,答错次数) " \
                          "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
                    vals = (self.name,) + x[1:] + (1,)
                    data.insert_singlerow_data(sql, vals)
            for x in right: # 做对，答错次数-1
                for y in my_wrong:
                    if (self.name, )+x[1:-1] == y[1:-2]:
                        if y[-1] > 1:
                            num = y[-1] - 1
                            sql = "update t_wrongtiku set 答错次数='" + str(num) + "' where ID='" + str(y[0]) + "'"
                            data.excuteSql(sql)
                        else:
                            data.excuteSql("delete from t_wrongtiku where ID='"+str(y[0])+"'")
            QMessageBox.about(self, "提示", "错题存储成功！")
        self.close()

    def keyReleaseEvent(self, evt) -> None:
        super(moniPane, self).keyReleaseEvent(evt)
        if evt.key() == 16777234:
            self.prev_clicked()
        elif evt.key() == 16777235:
            a = self.forward_btnNum - 25
            if a < 0:
                a += len(self.tkdata) - len(self.tkdata) % 25 + 25
                if a > len(self.tkdata)-1:
                    a = a - 25
            self.btn_pressed(a)
        elif evt.key() == 16777236:
            self.next_clicked()
        elif evt.key() == 16777237:
            a = self.forward_btnNum + 25
            if a > len(self.tkdata)-1:
                a = a % 25
            self.btn_pressed(a)
        else:
            pass

    # 上一题按钮槽函数
    def prev_clicked(self):
        if self.forward_btnNum == 0:
            self.btn_pressed(len(self.tkdata) - 1)
        else:
            self.btn_pressed(self.forward_btnNum-1)

    # 下一题按钮槽函数
    def next_clicked(self):
        if self.forward_btnNum == len(self.tkdata)-1:
            self.btn_pressed(0)
        else:
            self.btn_pressed(self.forward_btnNum+1)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    data = sqlite3Class("data.db")
    win = moniPane(data.selectdata("select * from t_timu"), "dll")
    win.show()
    sys.exit(app.exec_())
